import { prop, modelOptions, index, Ref } from '@typegoose/typegoose'
import { ApiProperty } from '@nestjs/swagger'

@modelOptions({
  schemaOptions: {
    _id: false, //不生成mongoDb默认的id
    // toJSON: { virtuals: true }, //显示虚拟属性
    // toObject: { virtuals: true },
  },
})
class MetaClass {
  /** 菜单名称 */
  @ApiProperty({ description: '菜单名称', required: false })
  @prop()
  title?: string

  /** 菜单图标  */
  @ApiProperty({ description: '菜单图标', required: false })
  @prop()
  icon?: string

  /** 菜单是否隐藏 */
  @ApiProperty({ description: '菜单是否隐藏', required: false })
  @prop()
  hidden?: boolean

  /** 是否需要经过身份验证 */
  @ApiProperty({ description: '是否需要经过身份验证', required: false })
  @prop()
  requiresAuth?: boolean

  /** 是否需要缓存 */
  @ApiProperty({ description: '是否需要缓存', required: false })
  @prop()
  keepAlive?: boolean

  /** 是否固定页签 */
  @ApiProperty({ description: '是否固定页签', required: false })
  @prop()
  affix?: boolean

  /** 是否在面包屑上隐藏 */
  @ApiProperty({ description: '是否在面包屑上隐藏', required: false })
  @prop()
  breadcrumb?: boolean

  /** 拥有菜单权限的角色编码集合 */
  @ApiProperty({ description: '拥有菜单权限的角色编码集合', required: false })
  @prop()
  roles?: string[]
}

@index({ path: 1 })
@modelOptions({
  schemaOptions: {
    collection: 'menus', //数据库集合的名称
    timestamps: true,
  },
})
export class CreateMenuDto {
  @ApiProperty({ description: '菜单路径', example: '/system' })
  @prop({ required: true, unique: true, match: /^\/\w+$/ })
  readonly path!: string

  @ApiProperty({ description: '组件路径', required: false })
  @prop()
  readonly component?: string

  @ApiProperty({
    description: '菜单名称',
    example: '系统管理',
    required: false,
  })
  @prop()
  readonly name?: string

  @ApiProperty({ description: '父节点id', required: false })
  @prop()
  readonly parentId?: string

  @ApiProperty({ description: '重定向', required: false })
  @prop()
  readonly redirect?: string

  @ApiProperty({ required: false })
  @prop({ ref: () => MetaClass })
  readonly meta?: Ref<MetaClass>
}
